Imports System.ComponentModel
Imports System.Web.UI

Namespace Controls
    <DefaultProperty("Text"), ToolboxData("<{0}:SimpleGrid runat=server></{0}:SimpleGrid>")> Public Class SimpleGrid
        Inherits DataGrid

        Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)

            If Me.AllowPaging Then
                Dim i As Integer
                Dim HTML As String
                Dim AlphabetHTML As String

                Dim pos, posTemp As Integer
                Dim content As New System.IO.StringWriter()

                MyBase.Render(New System.Web.UI.HtmlTextWriter(content))
                HTML = content.ToString()

                Dim startPos, endPos As Integer
                pos = HTML.LastIndexOf("<tr")
                pos = HTML.IndexOf(">", pos)
                If pos > -1 Then
                    Dim PrevInsertPos As Integer = HTML.IndexOf("<td", pos)
                    PrevInsertPos = HTML.IndexOf(">", PrevInsertPos) + 1

                    pos = HTML.IndexOf(">" & (Me.CurrentPageIndex + 1).ToString & "</", pos)

                    ' add Prev
                    If Me.CurrentPageIndex > 0 Then
                        startPos = HTML.LastIndexOf("a href=", pos)
                        If startPos > -1 Then
                            startPos += 7
                            endPos = HTML.IndexOf(">" & (Me.CurrentPageIndex).ToString & "</", startPos)

                            HTML = HTML.Insert(PrevInsertPos, String.Format("<a href={0}>PREVIOUS&nbsp;|</a>&nbsp;&nbsp;", HTML.Substring(startPos, endPos - startPos)))
                        End If
                    End If

                    pos = HTML.LastIndexOf("<tr")
                    pos = HTML.IndexOf(">", pos)
                    Dim NextInsertPos As Integer = HTML.IndexOf("</td>", pos)

                    pos = HTML.IndexOf(">" & (Me.CurrentPageIndex + 1).ToString & "</", pos)

                    ' add Next
                    If Me.CurrentPageIndex < Me.PageCount - 1 Then
                        startPos = HTML.IndexOf("a href=", pos)
                        If startPos > -1 Then
                            startPos += 7
                            endPos = HTML.IndexOf(">" & (Me.CurrentPageIndex + 2).ToString & "</", startPos)

                            HTML = HTML.Insert(NextInsertPos, String.Format("&nbsp;&nbsp;<a href={0}>|&nbsp;NEXT</a>", HTML.Substring(startPos, endPos - startPos)))
                        End If
                    End If

                End If

                writer.Write(HTML)
            Else
                MyBase.Render(writer)
            End If

        End Sub

        Protected Overrides Sub OnItemCreated(ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs)

            If e.Item.ItemType = WebControls.ListItemType.Pager Then
                Dim i As Integer
                Dim nextLink As String

                ' bold current page
                For i = 0 To e.Item.Cells(0).Controls.Count - 1 Step 2

                    Try
                        Dim lit As WebControls.Literal
                        lit = CType(e.Item.Cells(0).Controls(i), WebControls.Literal)
                        lit.Text = "<B>" & lit.Text & "</B>"
                    Catch
                        Try
                            Dim lblNum As WebControls.Label
                            lblNum = CType(e.Item.Cells(0).Controls(i), WebControls.Label)
                            lblNum.Text = "<B>" & lblNum.Text & "</B>"
                        Catch
                        End Try
                    End Try
                Next

                ' add Page
                Dim lblPage As New WebControls.Label()
                lblPage.Text = "PAGE&nbsp;&nbsp;"
                e.Item.Cells(0).Controls.AddAt(0, lblPage)

                '' add Prev
                'If Me.CurrentPageIndex > 0 Then
                '    Dim lbl As New WebControls.Label()
                '    lbl.Text = "&nbsp;&nbsp;"
                '    Dim c As New WebControls.LinkButton()
                '    c.CommandName = "Page"
                '    c.CommandArgument = 2
                '    'AddHandler c.Click, AddressOf onPrev_Click
                '    c.Text = "&lt;Prev"
                '    e.Item.Cells(0).Controls.AddAt(0, lbl)
                '    e.Item.Cells(0).Controls.AddAt(0, c)
                'End If

                '' add Next
                'If Me.CurrentPageIndex < Me.PageCount - 1 Then
                '    Dim lbl As New WebControls.Label()
                '    lbl.Text = "&nbsp;&nbsp;"
                '    Dim b As New WebControls.LinkButton()
                '    b.Text = "Next&gt;"
                '    Dim c As New WebControls.LinkButton()
                '    c.CommandName = "Page"
                '    c.CommandArgument = 2
                '    'AddHandler b.Click, AddressOf onNext_Click
                '    e.Item.Cells(0).Controls.Add(lbl)
                '    e.Item.Cells(0).Controls.Add(b)
                'End If
            End If
        End Sub

    End Class
End Namespace